#include "mutex.h"
#define _WINSOCKAPI_
#include <windows.h>
#include "elassert.h"

using namespace el;

Mutex::Mutex()
{
	win_mutex_ = CreateMutex(NULL,false,NULL);
	assert(win_mutex_);
}


Mutex::~Mutex()
{
	if (win_mutex_)
	{
		CloseHandle(win_mutex_);
	}
}

void el::Mutex::lock()
{
	assert(win_mutex_);
	WaitForSingleObject(win_mutex_, INFINITE);
}

void el::Mutex::unlock()
{
	assert(win_mutex_);
	ReleaseMutex(win_mutex_);
}

el::MutexLockGuard::MutexLockGuard(Mutex & mutex)
	:mutex_(mutex)
{
	mutex_.lock();
}

el::MutexLockGuard::~MutexLockGuard()
{
	mutex_.unlock();
}
